// pages/setOrg/setOrg.js
const app = getApp();
const qiniu = require("../../utils/qiniu.min.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orgId:"",
    isOrg:1,
    show:false,
    // orgSearch:true,
    showSearch:false,
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  // 获取组织详情
  detail: function () {
    var that = this;
    app.req({
      // url: "orgmini/org/detail",
      url: "orgmini/org/newGetOrgInfo",
      data: {
        orgId: that.data.orgId
      },
      success: function (res) {
        if (200 == res.code) {
          that.toggle("detail", res.data);
          that.toggle("imgCover", res.data.orgUrl);
          that.toggle("orgRecommend", !res.data.orgRecommend);
          that.toggle("orgSearch", res.data.orgSearch);
        }
      }
    })
  },
  toPage(e) {
    var that = this;
    var url = e.currentTarget.dataset.turn;
    var authStatus = e.currentTarget.dataset.authstatus;
    if (authStatus && authStatus == 1){
      wx.showModal({
        showCancel:false,
        title: '提示',
        content: '组织正在认证审核中，名称暂时无法修改',
      })
      return
    }
    url = url + "?orgId=" + that.data.orgId + "?authStatus=" + authStatus;
    console.log(url)
    wx.navigateTo({
      url: url
    })
  },
  // 组织解散/退出
  outOrg(){
    var that = this;
    wx.showModal({
      title: '提示',
      confirmText:"确认",
      cancelText:"再想想",
      content: that.data.isOrg == 5 ? '组织解散后，组织内相关内容会被删除，所有成员将被移除组织，是否确认要解散组织？' : '退出组织后，您将无法查看组织内任何内容，是否确认退出组织？',
      success(res) {
        if (res.confirm) {
          app.req({
            url: that.data.isOrg == 5 ? "orgmini/org/disbandedOrg" :"orgmini/org/signOutOrg",
            method: "post",
            data: {
              orgId: that.data.orgId,
              userId: that.data.isOrg == 5 ? '':that.data.userId
            },
            success(res) {
              if (res.code == 200) {
                wx.showToast({
                  title: that.data.isOrg == 5 ? '解散组织成功' :'退出组织成功',
                  icon:'none',
                  duration:3000
                })
                app.getRefresh(function (refresh) {
                }, '1')
                if (that.data.isOrg == 5){
                  wx.switchTab({
                    url: '/pages/newIndex/newIndex',
                  })
                }else{
                  wx.navigateBack()
                }
              }
            },
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onClose: function () {
    this.toggle("show", false);
  },
  openToApp(){
    this.toggle("show", true);
  },
  // 推荐按钮与否
  changeSwitch(e) {
    // console.log(e.detail.value * 1)
    var that = this;
    if (e.detail.value * 1 && !this.data.orgSearch){
      wx.showModal({
        title: '提示',
        showCancel:false,
        content: `允许组织被搜索才能允许推荐组织`,
        confirmText: "确认",
        success(res) {
          if (res.confirm) {
            that.toggle("orgRecommend", false);
          }
        }
      })
      return
    }
    app.req({
      url:"orgmini/org/updateOrgRecommend",
      method:"post",
      data:{
        "orgRecommend": !e.detail.value * 1,
        "id": that.data.orgId
      },
      success(res){
        if(res.code == 200){
          that.toggle("orgRecommend", e.detail.value * 1);
          wx.showToast({
            title: e.detail.value ? '推荐成功' :'取消推荐成功',
            icon:"none"
          })
        }
      }
    })
  },
  // 搜索按钮与否
  changeSwitchSeach(e) {
    console.log(e.detail.value * 1)
    var that = this;
    if (!e.detail.value){
      this.toggle("showSearch", true);
      this.toggle("searchValue", e.detail.value);
    }else{
      app.req({
        url: "orgmini/org/updateOrgSearch",
        method: "post",
        data: {
          "orgSearch": e.detail.value * 1,
          "id": that.data.orgId
        },
        success(res) {
          if (res.code == 200) {
            that.toggle("orgSearch", e.detail.value * 1);
            // wx.showToast({
            //   title: e.detail.value ? '搜索成功' : '取消搜索成功',
            //   icon: "none"
            // })
          }
        }
      })
    }
  },
  onSearchClose(){
    var value = this.data.searchValue
    console.log('点击确认啦',value)
    var that =this;
    if (that.data.orgRecommend == 1) {
      app.req({
        url: "orgmini/org/updateOrgRecommend",
        method: "post",
        data: {
          "orgRecommend": !value * 1,
          "id": that.data.orgId
        },
        success(res) {
          if (res.code == 200) {
            that.toggle("orgRecommend", false);
            // wx.showToast({
            //   title: e.detail.value ? '推荐成功' : '取消推荐成功',
            //   icon: "none"
            // })
          }
        }
      })
    }
    app.req({
      url: "orgmini/org/updateOrgSearch",
      method: "post",
      data: {
        "orgSearch": value * 1,
        "id": that.data.orgId
      },
      success(res) {
        if (res.code == 200) {
          that.toggle("orgSearch", value * 1);
          // wx.showToast({
          //   title: value ? '搜索成功' : '取消搜索成功',
          //   icon: "none"
          // })
        }
      }
    })
    this.toggle("showSearch", false);
  },
  onSearchCancel(){
    var value = this.data.searchValue;
    this.toggle("orgSearch", !value * 1);
    this.toggle("showSearch", false);
  },
  //更换头像
  posterimg() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album','camera'],
      success: function (res) {
        const filepath = res.tempFilePaths[0]
        const src = filepath
        wx.navigateTo({
          url: `/pages/cutFace/index?src=${src}`,
        })
      }
    })
  },
  // 头像接口更换
  changePoster(){
    app.req({
      url: "orgmini/org/updateOrg",
      method: "post",
      data: {
        "id": this.data.orgId,
        "orgUrl": this.data.imgCover
      },
      success(res) {
        if (res.code == 200) {
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.toggle("orgId", options.id);
    this.toggle("isOrg", options.isOrg);
    this.detail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.getUserInfo(function (userInfo) {
      that.toggle("userId", userInfo.id);
    })
    // if (this.data.isOrg ==5){
    //   that.changePoster();
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})